<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="咨询人员" prop="consultName">
        <el-input
          v-model="queryParams.consultName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纳税类型" prop="personId">
        <el-select v-model="queryParams.personId" placeholder="纳税类型" clearable>
          <el-option v-for="dict in dict.type.person_id" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="是否受理" prop="state">
        <el-select v-model="queryParams.state" placeholder="是否受理" clearable>
          <el-option v-for="dict in dict.type.state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>


      <!-- <el-form-item label="回复时间" prop="recoveryTime">
        <el-date-picker clearable
          v-model="queryParams.recoveryTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回复时间">
        </el-date-picker>
      </el-form-item> -->

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['consult:consult:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['consult:consult:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    <!-- 以下是遍历表单 -->
  <el-badge :value="weidu" class="item">
        <el-button size="small">未处理的消息</el-button>
    </el-badge>
    <el-table v-loading="loading" :data="consultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="咨询序号" align="center" prop="consultId" /> -->
      <el-table-column label="咨询人员" align="center" prop="consultName" />
      <el-table-column label="纳税类型" align="center" prop="personId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.person_id" :value="scope.row.personId"/>
        </template>
      </el-table-column>


      <el-table-column prop="iphone" label="手机号" align="center"   >
        <template slot-scope="scope">
  	      {{scope.row.iphone?scope.row.iphone.replace(/(\d{3})(\d{4})(\d{4})/,"$1****$3"):""}}
        </template>
      </el-table-column>
      <el-table-column label="收入(元)" align="center" prop="income" sortable />
      <el-table-column label="咨询的时间" align="center" prop="consultTime" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已受理" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="咨询结果" align="center" prop="result" />
      <el-table-column label="回复时间" align="center" prop="recoveryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.recoveryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="是否已删除" align="center" prop="wipe">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.delete_or_not" :value="scope.row.wipe"/>
        </template>
      </el-table-column> -->

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['consult:consult:edit']"
          >受理</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['consult:consult:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListTwo"
    />

    <!-- 添加或修改纳税咨询管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="咨询名称" prop="consultName">
          <el-input v-model="form.consultName"
            :disabled="true"
           placeholder="请输入咨询名称" />
        </el-form-item>


        <el-form-item label="类别" prop="personId">
              <el-select v-model="form.personId" placeholder="会员类别">
                <el-option
                  v-for="dict in dict.type.person_id"
                  :key="dict.value"
                  :label="dict.label"
                  :disabled="true"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
        </el-form-item>

        <el-form-item label="手机号" prop="iphone">
          <el-input v-model="form.iphone" :disabled="true" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="收入" prop="income">
          <el-input v-model="form.income" :disabled="true" placeholder="请输入收入" />
        </el-form-item>
        <el-form-item label="咨询的时间"  prop="consultTime">
          <el-date-picker clearable
            v-model="form.consultTime"
            type="date"
            value-format="yyyy-MM-dd"
            :disabled="true"
            placeholder="请选择咨询的时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="咨询内容" prop="description">
          <el-input v-model="form.description" :disabled="true" placeholder="请输入收入" />
        </el-form-item>



        <el-form-item label="是否已受理" prop="state">
              <el-select v-model="form.state" placeholder="是否已受理">
                <el-option
                  :disabled="true"
                  v-for="dict in dict.type.state"
                  :key="dict.value"
                  :label="dict.label"
                   :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
        </el-form-item>

        <el-form-item label="回复内容" prop="result">
          <el-input
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 4}"
            placeholder="请输入内容"
            v-model="form.result">
          </el-input>
          <!-- <el-input v-model="form.result" placeholder="请输入咨询结果" /> -->
        </el-form-item>

        <!-- <el-form-item label="回复时间" prop="recoveryTime">
          <el-date-picker clearable
            v-model="form.recoveryTime"
            type="date"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd"
            placeholder="请选择回复时间">
          </el-date-picker>
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"> 受 理 </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConsult,
  getConsult,
  delConsult,
  addConsult,
  updateConsult
} from "@/api/consult/consult";

export default {
  name: "Consult",
  dicts: ["person_id", "state","delete_or_not"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 纳税咨询管理表格数据
      consultList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consultName: null,
        personId: null,
        iphone: null,
        income: null,
        consultTime: null,
        state: null,
        result: null,
        recoveryTime: null,
        wipe: null,
        description:null,
      },
      weidu:0,
      // 表单参数
      form: {},
      //
      num:0,
      // 表单校验
      rules: {
        result:[
           { required: true, message: "回复不能为空", trigger: "blur" },
          { min: 2, message: "长度最少2个字符", trigger: "blur" },
        ],
      },
      pickerOptions: {
        disabledDate(v) {
          return v.getTime() < new Date().getTime() - 86400000;//  - 86400000是否包括当天
        }
      },
      aaa:{
        disabledDate(v) {
          return v.getTime() > new Date().getTime() //  - 86400000是否包括当天
        }
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询纳税咨询管理列表 */
    getList() {
      this.loading = true;
      listConsult(this.queryParams).then(response => {
        this.consultList = response.rows;
        this.total = response.total;
        this.loading = false;
        for (let index = 0; index < this.consultList.length; index++) {
            if(this.consultList[index].state ==1){
              this.weidu++
          }
    }
      });
    },
     /** 查询纳税咨询管理列表  此方法是点击受理以后调用遍历，为了让未读消息正常显示 */
    getListTwo() {
      this.loading = true;
      listConsult(this.queryParams).then(response => {
        this.consultList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        consultId: null,
        consultName: null,
        personId: null,
        iphone: null,
        income: null,
        consultTime: null,
        state: null,
        result: null,
        recoveryTime: null,
        wipe: null,
        description:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListTwo();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.consultId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加纳税咨询管理";
    },
    /** 受理按钮操作 */
    handleUpdate(row) {
      if(row.wipe ==1){
        this.$modal.msgWarning("已删除，无法操作");
      }else if(row.state ==0){
        this.$modal.msgWarning("已受理，请勿重复点击");
      }else{
        this.reset();
        const consultId = row.consultId || this.ids;
        getConsult(consultId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "受理纳税咨询";
      });
      }
    },
    /** 提交按钮 */
    submitForm() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.consultId != null) {
            updateConsult(this.form).then(response => {
              this.$modal.msgSuccess("受理成功");
              this.weidu--
              this.open = false;
              this.getListTwo();
            });
          } else {
            addConsult(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.state ==1){
        this.$modal.msgError("未受理的消息不可删除");
      }else if(row.wipe ==1){
         this.$modal.msgWarning("该信息已删除！请勿重复删除");
      }else{
          const consultIds = row.consultId || this.ids;
      this.$modal
        .confirm('是否确认删除纳税咨询管理编号为"' + consultIds + '"的数据项？')
        .then(function() {
          return delConsult(consultIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "consult/consult/export",
        {
          ...this.queryParams
        },
        `consult_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
